<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;

use Think\Controller;
use Org\Util\UcServer;

class IndexController extends Controller {
	protected $data;
	protected $UcServer;
	function __construct() {
		define ( 'UC_CONNECT', 'mysql' );
		define ( 'UC_DBHOST', 'rdsuzbru3uzbru3.mysql.rds.aliyuncs.com' );
		define ( 'UC_DBUSER', 'localhost' );
		define ( 'UC_DBPW', 'dbln4q78272k119o' );
		define ( 'UC_DBNAME', 'dbln4q78272k119o' );
		define ( 'UC_DBCHARSET', 'utf8' );
		define ( 'UC_DBTABLEPRE', '`dbln4q78272k119o`.devuc_' );
		define ( 'UC_DBCONNECT', '0' );
		define ( 'UC_KEY', '15FejiofaewjfioewoIOEFJeiowjeioawfji23FefioFJIefoEFiojFWOeifjIO4' );
		define ( 'UC_API', 'http://tsyuc.aliapp.com' );
		define ( 'UC_CHARSET', 'utf-8' );
		define ( 'UC_IP', '' );
		define ( 'UC_APPID', '1' );
		define ( 'UC_PPP', '20' );
		
		$this->UcServer = new UcServer ();
		if (! $this->UcServer) {
			exit ( "Error" );
			return false;
		}
		$this->data ['code'] = 200;
		$this->data ['message'] = "请求成功";
		dump($_POST);

		
		$_POST = $this->object_to_array(json_decode(uc_authcode($_POST['d'],'DECODE',UC_KEY)));
		dump($_POST);

	}
	
	/**
	 * 对象转数组函数
	 *
	 * @param object $obj
	 * @return array
	 * @author yanpengquan<490523604@qq.com> http://bbs.xinyustudio.cn
	 */
	function object_to_array($obj) {
		// 用三目运算符判定是否为object，若是则调用函数返回数组
		$array = is_object($obj) ? get_object_vars($obj) : $obj;
		// 用foreach来遍历该数组
		foreach ($array as $key => $val) {
			// 为节省代码，采用递归来完成解析
			$val = (is_array($val)) || is_object($val) ? $this->object_to_array($val) : $val;
			// 赋值
			$arr [$key] = $val;
		}
		return $arr;
	}
	
	
	public function index() {
		echo "欢迎使用由碳素云信息技术有限责任公司提供的Ucenter服务";
	}
	
	/**
	 * 用户注册
	 *
	 * 本接口函数用于新用户的注册。用户名、密码、Email 为一个用户在 UCenter 的基本数据，提交后 UCenter
	 * 会按照注册设置和词语过滤的规则检测用户名和 Email 的格式是否正确合法，如果正确则返回注册后的用户 ID，否则返回相应的错误信息。
	 *
	 * @param string $username
	 *        	用户名
	 * @param string $password
	 *        	密码
	 * @param string $email
	 *        	电子邮件
	 * @param integer $questionid
	 *        	安全提问索引
	 * @param string $answer
	 *        	安全提问答案
	 * @return mixed 大于 0:返回用户 ID，表示用户注册成功
	 *         -1:用户名不合法
	 *         -2:包含不允许注册的词语
	 *         -3:用户名已经存在
	 *         -4:Email 格式有误
	 *         -5:Email 不允许注册
	 *         -6:该 Email 已经被注册
	 */
	function uc_user_register() {
		$username = I ( 'username' );
		$password = I ( 'password' );
		$email = I ( 'email' );
		$questionid = I ( 'questionid' );
		$answer = I ( 'post.answer' );
		$this->data ['data'] = uc_user_register ( $username, $password, $email, $questionid ? $questionid : '', $answer ? $answer : '' );
		switch ($this->data ['data']) {
			case - 1 :
				$this->data ['message'] = "用户名不合法";
				$this->data ['code'] = '-1';
				break;
			case - 2 :
				$this->data ['message'] = "包含不允许注册的词语";
				$this->data ['code'] = '-2';
				break;
			case - 3 :
				$this->data ['message'] = "用户名已经存在";
				$this->data ['code'] = '-3';
				break;
			case - 4 :
				$this->data ['message'] = "Email 格式有误";
				$this->data ['code'] = '-4';
				break;
			case - 5 :
				$this->data ['message'] = "Email 不允许注册";
				$this->data ['code'] = '-5';
				break;
			case - 6 :
				$this->data ['message'] = "该 Email 已经被注册";
				$this->data ['code'] = '-6';
				break;
			default :
				break;
		}
		$this->curlRetrun ();
	}
	
	/**
	 * 用户登录
	 *
	 * @param string $username
	 *        	用户名 post
	 * @param string $password
	 *        	用户登录密码 post
	 * @param number $isuid
	 *        	是否使用uid登陆，默认为否，值为0，post
	 * @param number $checkques
	 *        	是否验证安全提问 默认为否
	 * @param number $questionid
	 *        	安全提问编号 number
	 * @param string $answer
	 *        	安全提问答案 string
	 * @return integer [0]	大于 0:返回用户 ID，表示用户登录成功
	 *         -1:用户不存在，或者被删除
	 *         -2:密码错误
	 *         -3:安全提问错误
	 *         string [1]	用户名
	 *         string [2]	密码
	 *         string [3]	Email
	 *         bool [4]	用户名是否重名
	 *         示例：
	 *        
	 *         {"is_repet":0,"email":"15648@qq.com","password":"fjowiei","username":"fwjeiojwie","uid":"1"}
	 *         用户名未重复，电子邮件地址，密码，用户名，uid
	 */
	function uc_user_login() {
		$username = I ( 'username' );
		$password = I ( 'password' );
		$isuid = I ( 'isuid', 0 );
		$checkques = I ( 'checkques', 0 );
		$questionid = I ( 'questionid', '' );
		$answer = I ( 'post.answer', '' );
		list ( $array ['uid'], $array ['username'], $array ['password'], $array ['email'], $array ['is_repet'] ) = uc_user_login ( $username, $password, $isuid, $checkques, $questionid, $answer );
		
		$this->data ['data'] = $array ['uid'];
		switch ($this->data ['data']) {
			case - 1 :
				$this->data ['message'] = "用户不存在，或者被删除";
				$this->data ['code'] = '-1';
				break;
			case - 2 :
				$this->data ['message'] = "密码错误";
				$this->data ['code'] = '-2';
				break;
			case - 3 :
				$this->data ['message'] = "安全提问错误";
				$this->data ['code'] = '-3';
				break;
		}
		$this->curlRetrun ();
	}
	
	/**
	 * 获取用户数据
	 *
	 * @param string $username
	 *        	用户名 post
	 * @param bool $isuid
	 *        	是否使用用户 ID获取 post
	 *        	1:使用用户 ID获取
	 *        	0:(默认值) 使用用户名获取
	 * @return integer [0]	用户 ID
	 *         string [1]	用户名
	 *         string [2]	Email
	 *         示例：
	 *        
	 *         {"email":"807572915@qq.com","username":"wutian","id":"3"}
	 *         电子邮箱地址，用户名，用户ID号
	 */
	function uc_get_user() {
		$username = I ( 'username' );
		$isuid = I ( 'isuid', 0 );
		list ( $array ['id'], $array ['username']) = uc_get_user( $username, $isuid);
		echo json_encode ( $array );
	}
	
	/**
	 * 更新用户资料
	 *
	 * @param string $username;
	 *        	用户名 post
	 * @param string $oldpw;
	 *        	旧密码 post
	 * @param string $newpw
	 *        	新密码 post
	 * @param string $email;
	 *        	电子邮箱地址 post
	 * @param bool $ignoreoldpw
	 *        	是否忽略旧密码 默认值 0,不忽略，更改资料需要验证密码
	 * @param number $questionid
	 *        	安全提问索引 post
	 * @param string $answer
	 *        	安全提问答案 post
	 * @return integer 1:更新成功
	 *         0:没有做任何修改
	 *         -1:旧密码不正确
	 *         -4:Email 格式有误
	 *         -5:Email 不允许注册
	 *         -6:该 Email 已经被注册
	 *         -7:没有做任何修改
	 *         -8:该用户受保护无权限更改
	 *         示例：
	 *        
	 *        
	 */
	function uc_user_edit() {
		$username = I ( 'username' );
		$oldpw = I ( 'oldpw' );
		$newpw = I ( 'newpw' );
		$email = I ( 'email' );
		$ignoreoldpw = I ( 'ignoreoldpw', 0 );
		$questionid = I ( 'questionid', '' );
		$answer = I ( 'answer', '' );
		list ( $array ['username'], $array ['oldpw'], $array ['newpw'], $array ['email'], $array ['ignoreoldpw'], $array ['questionid'], $array ['answer'] ) = uc_user_edit ( $username, $oldpw, $newpw, $email, $ignoreoldpw, $questionid, $answer );
		$this->data ['data'] = uc_user_edit ( $username, $oldpw, $newpw, $email, $ignoreoldpw, $questionid, $answer );
		// echo json_encode ( $array );
		switch ($$this->data ['data']) {
			case 0 :
				$this->data ['message'] = "没有做任何修改";
				$this->data ['code'] = '0';
			case - 1 :
				$this->data ['message'] = "旧密码不正确";
				$this->data ['code'] = '-1';
			case - 4 :
				$this->data ['message'] = "Email 格式有误";
				$this->data ['code'] = '-4';
			case - 5 :
				$this->data ['message'] = "Email 不允许注册";
				$this->data ['code'] = '-5';
			case - 6 :
				$this->data ['message'] = "该 Email 已经被注册";
				$this->data ['code'] = '-6';
			case - 7 :
				$this->data ['message'] = "没有做任何修改";
				$this->data ['code'] = '-7';
			case - 8 :
				$this->data ['message'] = "该用户受保护无权限更改";
				$this->data ['code'] = '-8';
		}
		$this->curlRetrun ();
	}
	
	/**
	 * 删除用户
	 *
	 * @param integer $uid;
	 *        	用户名 post
	 * @return integer 1:成功
	 *         0：失败
	 *         示例：
	 *        
	 *        
	 */
	function uc_user_delete() {
		$uid = I ( 'uid' );
		$delete_user = uc_user_delete ( $uid );
		$this->data ['data'] = uc_user_delete ( $uid );
		switch ($this->data ['data']) {
			case 0 :
				$this->data ['message'] = "删除失败";
				$this->data ['code'] = '0';
				break;
			case 1 :
				$this->data ['message'] = "删除成功";
				$this->data ['code'] = '1';
				break;
		}
		// echo json_encode ( $delete_user );
		$this->curlRetrun ();
	}
	
	/**
	 * 删除用户头像
	 *
	 * @param integer $uid;
	 *        	用户名 post
	 * @return integer 1:成功
	 *         0：失败
	 */
	function uc_user_deleteavatar() {
		$uid = I ( 'uid' );
		$delete ['del'] = uc_user_deleteavatar ( $uid );
		$this->data ['data'] = uc_user_deleteavatar ( $uid );
		switch ($this->data ['data']) {
			case 0 :
				$this->data ['message'] = "删除失败";
				$this->data ['code'] = '0';
				break;
			case 1 :
				$this->data ['message'] = "删除成功";
				$this->data ['code'] = '1';
				break;
		}
		$this->curlRetrun ();
		// echo json_encode ( $delete );
	}
	
	/**
	 * 同步登录
	 *
	 * @param integer $uid;
	 *        	用户ID post
	 * @return string 同步登录的 HTML 代码
	 *         示例：
	 *        
	 *        
	 */
	function uc_user_synlogin() {
		$uid = I ( 'uid' );
		$synlogin = uc_user_synlogin ( $uid );
		echo json_encode ( $synlogin );
	}
	
	/**
	 * 同步退出
	 *
	 * @return string 同步退出的 HTML 代码
	 *         示例：
	 *        
	 */
	function uc_user_synlogout() {
		$synlogout ['synlogout'] = uc_user_synlogout ();
		echo json_encode ( $synlogout );
	}
	
	/**
	 * 检查 Email 地址
	 *
	 * @param string $email
	 *        	邮箱地址 post
	 * @return integer 1:成功
	 *         -4:Email 格式有误
	 *         -5:Email 不允许注册
	 *         -6:该 Email 已经被注册
	 *         示例：
	 *        
	 *        
	 */
	function uc_user_checkemail() {
		$email = I ('email');
		$checkmail ['checkmail'] = uc_user_checkemail ($email);
		$this->data ['data'] = uc_user_checkemail ($email);
		switch ($this->data ['data'])
		 {
			case - 4 :
				$this->data ['message'] = "格式有误";
				$this->data ['code'] = '-4';
				break;
			case - 5 :
				$this->data ['message'] = "不允许注册";
				$this->data ['code'] = '-5';
				break;
			case - 6 :
				$this->data ['message'] = "已经被注册";
				$this->data ['code'] = '-6';
				break;
		}
	    echo $email;
		$this->curlRetrun ();
		//echo json_encode ( $checkmail );
	}
	
	/**
	 * 检查用户名
	 *
	 * @param string $username;
	 *        	用户名 post
	 * @return integer 1:成功
	 *         -1:用户名不合法
	 *         -2:包含要允许注册的词语
	 *         -3:用户名已经存在
	 *         示例：
	 *        
	 */
	function uc_user_checkname() {
		$username = I ( 'username' );
		$checkname ['checkname'] = uc_user_checkname ( $username );
		$this->data ['data'] = uc_user_checkname ( $username );
		switch ($this->data ['data']) {
			case - 1 :
				$this->data ['message'] = "用户名不合法";
				$this->data ['code'] = '-1';
				break;
			case - 2 :
				$this->data ['message'] = "包含要允许注册的词语";
				$this->data ['code'] = '-2';
				break;
			case - 3 :
				$this->data ['message'] = "用户名已经存在";
				$this->data ['code'] = '-3';
				break;
		}
		$this->curlRetrun ();
		// echo json_encode ( $checkname );
	}
	
	/**
	 * 添加保护用户
	 *
	 * @param string $username;
	 *        	用户名 post
	 * @param string $admin
	 *        	操作的管理员 post
	 * @return integer 1:成功
	 *         -1：失败
	 *         示例：
	 */
	function uc_user_addprotected() {
		$username = I ( 'username' );
		$admin = I ( 'admin' );
		$addprotected ['addprotected'] = uc_user_addprotected ( $username, $admin );
		$this->data ['data'] = uc_user_addprotected ( $username, $admin );
		switch ($this->data ['data']) {
			case 1 :
				$this->data ['message'] = "成功";
				$this->data ['code'] = '1';
				break;
			case - 1 :
				$this->data ['message'] = "失败";
				$this->data ['code'] = '-1';
				break;
		}
		$this->curlRetrun ();
		// echo json_encode ( $addprotected );
	}
	
	/**
	 * 删除保护用户
	 *
	 * @return integer 1:成功
	 *         -1：失败
	 *         示例：
	 *        
	 */
	function uc_user_deleteprotected() {
		$deleteprotected ['deleteprotected'] = uc_user_deleteprotected ();
		$this->data ['data'] = uc_user_deleteprotected ();
		switch ($this->data ['data']) {
			case 1 :
				$this->data ['message'] = "成功";
				$this->data ['code'] = '1';
				break;
			case - 1 :
				$this->data ['message'] = "失败";
				$this->data ['code'] = '-1';
				break;
		}
		$this->curlRetrun ();
		// echo json_encode ( $deleteprotected );
	}
	
	/**
	 * 得到受保护的用户名列表
	 *
	 * @return array 受保护的用户数据
	 *         示例：
	 *        
	 */
	function uc_user_getprotected() {
		$getprotected ['getprotected'] = uc_user_getprotected ();
		echo json_encode ( $getprotected );
	}
	
	/**
	 * 把重名用户合并到 UCenter
	 *
	 * @param string $username
	 *        	老用户名 post
	 * @param string $newusername
	 *        	新用户名 post
	 * @param integer $uid;
	 *        	用户ID post
	 * @param string $password
	 *        	密码 post
	 * @param string $email
	 *        	电子邮箱地址 post
	 * @return integer 大于 0:返回用户 ID，表示用户注册成功
	 *         -1:用户名不合法
	 *         -2:包含不允许注册的词语
	 *         -3:用户名已经存在
	 *         示例：
	 *        
	 */
	function uc_user_merge() {
		$oldusername = I ( 'oldusername' );
		$newusername = I ( 'newusername' );
		$uid = I ( ' uid' );
		$password = I ( 'password' );
		$email = I ( 'email' );
		$merge ['merge'] = uc_user_merge ( $oldusername, $newusername, $uid, $password, $email );
		$this->data ['data'] = uc_user_merge ( $oldusername, $newusername, $uid, $password, $email );
		switch ($this->data ['data']) {
			case - 1 :
				$this->data ['message'] = "用户名不合法";
				$this->data ['code'] = '-1';
				break;
			case - 2 :
				$this->data ['message'] = "包含不允许注册的词语";
				$this->data ['code'] = '-2';
				break;
			case - 3 :
				$this->data ['message'] = "用户名已经存在";
				$this->data ['code'] = '-3';
				break;
		}
		$this->curlRetrun ();
		// echo json_encode ( $merge );
	}
	
	/**
	 * 移除重名用户记录
	 *
	 * @param string $username        	
	 * @return mixed
	 */
	function uc_user_merge_remove() {
		$username = I ( 'username' );
		$remove ['remove'] = uc_user_merge_remove ( 'username' );
	}
	
	/**
	 * 获取指定应用的指定用户积分
	 *
	 * @param integer $appid
	 *        	应用ID post
	 * @param integer $uid
	 *        	用户ID post
	 * @param integer $credit
	 *        	积分编号 post
	 * @return integer 积分
	 *         示例：
	 */
	function uc_user_getcredit() {
		$appid = I ( 'appid' );
		$uid = I ( 'uid' );
		$credit = I ( 'credit' );
		$getcredit ['getcredit'] = uc_user_getcredit ( $appid, $uid, $credit );
		echo json_encode ( $getcredit );
	}
	
	function curlRetrun() {
		echo json_encode ( $this->data );
	}
	
	function uc_authcode()
	{
		$string = "wutian";
		$operation = 'ENCODE';
		$key = '';
		$expiry = 0;
		$str = uc_authcode($string, $operation, $key, $expiry);
		echo $str;
		$jiemi = uc_authcode($str,'DECODE',$key, $expiry);
		echo $jiemi;
	}
}   